package com.dwxt.cdms.util;


import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LbWatchCodeTool {


    private static final String TAG = "LbWatchCodeTool";


    /**
     * 数据类型：卡号
     */
    private static final String TYPE_CCID = "01";

    /**
     * 数据长度：卡号
     */
    private static int LEN_CCID = 20;

    /**
     * 数据类型：IMEI
     */
    private static final String TYPE_IMEI = "02";

    /**
     * 数据长度：IMEI
     */
    private static int LEN_IMEI = 15;

    /**
     * 数据类型：MAC
     */
    private static final String TYPE_MAC = "03";

    /**
     * 数据长度：MAC
     */
    private static int LEN_MAC = 12;

    /**
     * 数据类型：版本号
     */
    private static final String TYPE_VERSION = "04";

    /**
     * 数据长度：版本号
     */
    private static int LEN_VERSION = 9;

    /**
     * 数据类型：运行模式
     */
    private static final String TYPE_MODE = "05";

    /**
     * 数据长度：运行模式
     */
    private static int LEN_CARD_MODE = 8;

    /**
     * 数据类型：完整的设备名字
     */
    private static final String TYPE_NAME = "06";

    /**
     * 数据长度：完整的设备名字
     */
    private static int LEN_NAME= 14;//目前1.5代 14字节


    private String deviceName;
    private String cardMode;
    private String cardNo;
    private String version;
    private String mac;
    private String imei;
    private String generation;

    private LbWatchCodeTool() {
    }


    private LbWatchCodeTool(String deviceName, String cardMode, String cardNo, String version, String mac, String imei, String generation) {
        this.deviceName = deviceName;
        this.cardMode = cardMode;
        this.cardNo = cardNo;
        this.version = version;
        this.mac = mac;
        this.imei = imei;
        this.generation = generation;
    }

    /**
     *
     * @param codeData 二维码数据
     * @return LbWatchCodeTool实体数据类,LbWatchCodeTool不为null，LbWatchCodeTool下的参数可能为null
     */
    public static LbWatchCodeTool parse(String codeData) {
        String deviceName = null;
        String cardMode = null;
        String cardNo = null;
        String version = null;
        String mac = null;
        String imei = null;
        String generation = "-1";

        if (codeData == null) {
            return null;
        }

        String[] tmp = codeData.split("/");
        if(tmp.length == 4 || tmp.length == 5){
            if(tmp[0].length() != LEN_CCID || tmp[1].length() != LEN_IMEI || tmp[2].length() != LEN_MAC
                    || tmp[3].length() != LEN_VERSION || (tmp.length == 5 && tmp[4].length() != LEN_CARD_MODE)){
                return null;
            }
            cardNo = tmp[0];
            imei = tmp[1];
            mac = tmp[2].substring(0,2) + ":"
                    + tmp[2].substring(2,4) + ":"
                    + tmp[2].substring(4,6) + ":"
                    + tmp[2].substring(6,8) + ":"
                    + tmp[2].substring(8,10) + ":"
                    + tmp[2].substring(10,12);
            version = tmp[3];
            if(tmp.length == 5) {
                cardMode = tmp[4];
            }
            return new LbWatchCodeTool("NBWR-" + version,cardMode,cardNo,version,mac,imei,generation);
        }

        int currentPos = 0;
        try {
            boolean isTypeLenFlag = false;//数据类型与长度是否一致
            while (currentPos < codeData.length()) {
                isTypeLenFlag = false;
                String fieldType = codeData.substring(currentPos,currentPos+2);
                currentPos += 2;
                int dataLength = Integer.valueOf(codeData.substring(currentPos,currentPos+2));
                currentPos += 2;
                String data = codeData.substring(currentPos,currentPos+dataLength);
                switch (fieldType) {
                    case TYPE_CCID:
                        if(dataLength == LEN_CCID){
                            isTypeLenFlag = true;
                            cardNo = data;
                        }
                        break;
                    case TYPE_IMEI:
                        if(dataLength == LEN_IMEI){
                            isTypeLenFlag = true;
                            imei = data;
                        }
                        break;
                    case TYPE_MAC:
                        if(dataLength == LEN_MAC){
                            isTypeLenFlag = true;
                            mac = data.substring(0,2) + ":"
                                    + data.substring(2,4) + ":"
                                    + data.substring(4,6) + ":"
                                    + data.substring(6,8) + ":"
                                    + data.substring(8,10) + ":"
                                    + data.substring(10,12);;
                        }
                        break;
                    case TYPE_VERSION:
                        if(dataLength == LEN_VERSION){
                            isTypeLenFlag = true;
                            version = data;
                        }
                        break;
                    case TYPE_MODE:
                        if(dataLength == LEN_CARD_MODE){
                            isTypeLenFlag = true;
                            cardMode = data;
                        }
                        break;
                    case TYPE_NAME:
                        if(dataLength == LEN_NAME){
                            isTypeLenFlag = true;
                            deviceName = data;
                        }
                        break;
                    default:
                        break;
                }
                currentPos += dataLength;
                if(!isTypeLenFlag){
                    return null;
                }
            }
            //如果缺失必须的数据，则数据非法
            if(deviceName == null || cardMode == null || imei == null || mac == null){
                log.error(TAG, "非法的二维码数据");
                return null;
            }
            generation = deviceName.substring(5,6);
            String versionOfName = deviceName.substring(6);
            if(version == null){
                version = deviceName.substring(6);
            }else{
                if(!version.equals(versionOfName)) {
                    log.error(TAG, "非法的二维码数据");
                    return null;
                }
            }
            return new LbWatchCodeTool(deviceName,cardMode,cardNo,version,mac,imei,generation);
        } catch (Exception e) {
            log.error(TAG, "非法的二维码数据");
            return null;
        }
    }


    public String getDeviceName() {
        return deviceName;
    }

    public String getCardMode() {
        return cardMode;
    }

    public String getCardNo() {
        return cardNo;
    }

    public String getVersion() {
        return version;
    }

    public String getMac() {
        return mac;
    }

    public String getImei() {
        return imei;
    }

    public String getGeneration() {
        return generation;
    }
}
